/***************************AT24C02 Driver********************************/
/**
 * Date :2014.04.05
 */
#include"../Driver_PIC/system.h"
#include"../Driver_PIC/pic18_i2c.h"

    unsigned char At24c_WriteData = 0;
    unsigned char At24c_ReadData  = 0;
    unsigned char At24c_WriteBuff[32] =
    {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    unsigned char At24c_ReadBuff[64]=
    {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
/**************************************************************************/
signed char At24cReadByte(unsigned char address,unsigned char RomAdd ,unsigned char *data)
{
    SSP1CON2bits.SEN = 1;            // initiate bus start condition
    while(SSP1CON2bits.SEN);

    SSP1BUF = address;               // write single byte to SSP1BUF
    while( SSP1STATbits.BF );
    I2C1Idle ;

    if(SSP1CON2bits.ACKSTAT)         //如果EEROM内部忙碌，返回无应答标记
    {
        SSP1CON2bits.PEN = 1;
        while(SSP1CON2bits.PEN);
        return (-2);
    }
    SSP1BUF = RomAdd;
    while( SSP1STATbits.BF );
    I2C1Idle ;

    address ++;
    SSP1CON2bits.RSEN = 1;           // initiate bus restart condition
    while(SSP1CON2bits.RSEN);

    SSP1BUF = address;               // write single byte to SSP1BUF
    while( SSP1STATbits.BF );
    I2C1Idle ;

    SSP1CON2bits.RCEN = 1;           // enable master for 1 byte reception
    while ( !SSP1STATbits.BF );      // wait until byte received
    *data = SSP1BUF ;                 // return with read byte
    
    SSP1CON2bits.PEN = 1;
    while(SSP1CON2bits.PEN);
    return 0;
}
/**************************************************************************/
signed char At24cReadBytes(unsigned char address ,unsigned char RomAdd,unsigned char num ,unsigned char *Des)
{
    SSP1CON2bits.SEN = 1;            // initiate bus start condition
    while(SSP1CON2bits.SEN);

    SSP1BUF = address;               // write single byte to SSP1BUF
    while( SSP1STATbits.BF );
    I2C1Idle ;

    if(SSP1CON2bits.ACKSTAT)         //如果EEROM内部忙碌，返回无应答标记
    {
        SSP1CON2bits.PEN = 1;
        while(SSP1CON2bits.PEN);
        return (-2);
    }

    SSP1BUF = RomAdd;
    while( SSP1STATbits.BF );
    I2C1Idle ;

    address ++;
    SSP1CON2bits.RSEN = 1;           // initiate bus restart condition
    while(SSP1CON2bits.RSEN);

    SSP1BUF = address;               // write single byte to SSP1BUF
    while( SSP1STATbits.BF );
    I2C1Idle ;
    
    while(num --)
    {
        SSP1CON2bits.RCEN = 1;           // enable master for 1 byte reception
        while ( !SSP1STATbits.BF );      // wait until byte received
        (*Des++) = SSP1BUF ;                 // return with read byte
        
        while ( SSP1CON2bits.RCEN );     // check that receive sequence is over
        if ( num )                    // test if 'length' bytes have been read
        {
            SSP1CON2bits.ACKDT = 0;      // set acknowledge bit state for ACK
            SSP1CON2bits.ACKEN = 1;      // initiate bus acknowledge sequence
            while ( SSP1CON2bits.ACKEN );   // wait until ACK sequence is over
        }
    }
    return 0;
}
/***************************************************************************/
signed char At24cWriteByte(unsigned char address ,unsigned char RomAdd,unsigned char data)
{

    SSP1CON2bits.SEN = 1;            // initiate bus start condition
    while(SSP1CON2bits.SEN);

    SSP1BUF = address;               // write single byte to SSP1BUF
    while( SSP1STATbits.BF );
    I2C1Idle ;

    if(SSP1CON2bits.ACKSTAT)         //如果EEROM内部忙碌，返回无应答标记
    {
        SSP1CON2bits.PEN = 1;
        while(SSP1CON2bits.PEN);
        return (-2);
    }

    SSP1BUF = RomAdd;
    while( SSP1STATbits.BF );
    I2C1Idle ;

    SSP1BUF = data;
    while( SSP1STATbits.BF );
    I2C1Idle ;

    SSP1CON2bits.PEN = 1;
    while(SSP1CON2bits.PEN);
    return 0;
}
/***************************************************************************/
signed char At24cWriteBytes(unsigned char address, unsigned char RomAdd ,unsigned char num ,unsigned char *buff)
{

    SSP1CON2bits.SEN = 1;            // initiate bus start condition
    while(SSP1CON2bits.SEN);

    SSP1BUF = address;               // write single byte to SSP1BUF
    while( SSP1STATbits.BF );
    I2C1Idle ;

    if(SSP1CON2bits.ACKSTAT)         //如果EEROM内部忙碌，返回无应答标记
    {
        SSP1CON2bits.PEN = 1;
        while(SSP1CON2bits.PEN);
        return (-2);
    }

    SSP1BUF = RomAdd;
    while( SSP1STATbits.BF );
    I2C1Idle ;

    while ( num -- )                   // transmit data until null character
    {
         SSP1BUF = *buff++;            // write single byte to SSP1BUF
         while( SSP1STATbits.BF );     // wait until write cycle is complete
	 I2C1Idle;                     // ensure module is idle
    }                                  // continue data writes until null character

    SSP1CON2bits.PEN = 1;
    while(SSP1CON2bits.PEN);
    return 0;
}
/***************************************************************************/